package com.zzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.domain.Category;
import com.zzx.domain.Vo.AddCategoriesVo;
import com.zzx.domain.Vo.CategoryList;
import com.zzx.exception.ExceptionState;
import com.zzx.exception.SystemException;
import com.zzx.mapper.CategoryMapper;
import com.zzx.service.CategoryService;
import com.zzx.util.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2022-08-01 19:40:52
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    /**
     * 商品分类列表
     * @param type
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public ResponseResult getCategoriesList(Integer type, Integer pagenum, Integer pagesize) {
        Page<Category> categoryPage = null;
        if(Objects.nonNull(pagenum)||Objects.nonNull(pagesize)) {
             categoryPage = new Page<>(pagenum, pagesize);
        }

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        /*第一层的*/
        wrapper.eq(Category::getCatLevel,0);


        List<Category> records =null;
            if(Objects.nonNull(categoryPage)){
                page(categoryPage,wrapper);
                records = categoryPage.getRecords();
            }else {
                records =  list(wrapper);
            }
        List<Category> categoryS1 = null;
        List<Category> categoryS2 = null;
         if(type==null){
             /*获取第二层和第三次层的*/
             categoryS1 = getCategoryS(1);
             categoryS2 = getCategoryS(2);
         }
         else if(type==2){
                 // 获取第二层的
                 categoryS1 = getCategoryS(1);
             }else if(type==3){
                 /*获取第二层和第三次层的*/
                 categoryS1 = getCategoryS(1);
                 categoryS2 = getCategoryS(2);
             }



        List<Category> finalCategoryS = categoryS1;
        List<Category> finalCategoryS1 = categoryS2;
        List<CategoryList> collect = records.stream().map(item ->
                new CategoryList(item.getCatId(), item.getCatName(), item.getCatPid(), item.getCatLevel(),item.getCatDeleted()==0?false:true,
                        finalCategoryS==null?null:finalCategoryS.stream().filter(i2->i2.getCatPid()==(int)item.getCatId()).map(item2->
                                new CategoryList(item2.getCatId(), item2.getCatName(), item2.getCatPid(), item2.getCatLevel(),item2.getCatDeleted()==0?false:true,
                                        finalCategoryS1==null?null:finalCategoryS1.stream().filter(i3->i3.getCatPid()==(int)item2.getCatId()).map(item3->
                                                new CategoryList(item3.getCatId(), item3.getCatName(), item3.getCatPid(), item3.getCatLevel(),item3.getCatDeleted()==0?false:true,
                                                        null
                                                )).collect(Collectors.toList())
                                )).collect(Collectors.toList())
        )).collect(Collectors.toList());
        Map map = new HashMap();
        map.put("result",collect);
       if(Objects.nonNull(categoryPage)){
           map.put("total",categoryPage.getTotal());
       }

        return new ResponseResult("获取成功", ExceptionState.OK,map);
    }

    /**
     * 添加商品分类
     * @param categoriesVo
     * @return
     */
    @Override
    public ResponseResult addCategories(AddCategoriesVo categoriesVo) {
        Category category = new Category();
        category.setCatPid(categoriesVo.getCat_pid());
        category.setCatName(categoriesVo.getCat_name());
        category.setCatDeleted(0);
        category.setCatLevel(categoriesVo.getCat_level());
        boolean save = save(category);
        if(save==false) throw new SystemException(ExceptionState.UNCRESTEROLE);

        AddCategoriesVo addCategoriesVo = new AddCategoriesVo(category.getCatId(), categoriesVo.getCat_name(), category.getCatPid(), categoriesVo.getCat_level());


        return new ResponseResult("创建成功",ExceptionState.CREATED,addCategoriesVo);
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @Override
    public ResponseResult selectCategoriesById(Integer id) {
        Category byId = getById(id);
        AddCategoriesVo addCategoriesVo = new AddCategoriesVo(byId.getCatId(), byId.getCatName(), byId.getCatPid(), byId.getCatLevel());
        return new ResponseResult("获取成功",ExceptionState.OK,addCategoriesVo);
    }

    /**
     * 根据分类id提交数据
     * @param id
     * @return
     */
    @Override
    public ResponseResult updateCategoriesById(Integer id,AddCategoriesVo addCategoriesVo) {
        Category category = new Category();
        category.setCatId(id);
        category.setCatName(addCategoriesVo.getCat_name());
        boolean b = updateById(category);
        if(b==false) throw new SystemException(ExceptionState.NOUPDATE);
        Category byId = getById(id);
        AddCategoriesVo addCategoriesVo1 = new AddCategoriesVo(byId.getCatId(), byId.getCatName(), byId.getCatPid(), byId.getCatLevel());
        return new ResponseResult("更新成功",ExceptionState.OK,addCategoriesVo1);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteCategoriesById(Integer id) {
        boolean remove = removeById(id);
        if(remove==false) throw new SystemException(ExceptionState.NODELETE);
        return new ResponseResult("删除成功",ExceptionState.OK);
    }




    public List<Category> getCategoryS(Integer v){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getCatLevel,v);
        List<Category> list = list(wrapper);
        return list;
    }

}

